<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends Controller
{

    //登录页面
    public function login() {
        if (IS_POST) {
            $name = I('post.username');
            $pwd = I('post.password');

            $MODEL = M('admin');
            $where['username'] = $name;
            $where['password'] = md5($pwd);
            $data = $MODEL->field('id,username')->where($where)->find();
            if ($data) {
                $_SESSION['admin_username'] = $name;
                //写入session
                session(C("USER_AUTH_KEY"), $data["id"]);
                session("username", $data["username"]);
                session("logintime", $data["logintime"]);
                session("admin_id", $data["id"]);

                //如果为超级管理员，则无需验证
                if($name == C('ADMIN_AUTH_KEY')){
                    session(C('ADMIN_AUTH_KEY'),$name);
                }else{
                    \Org\Util\Rbac::saveAccessList($data['id']);
                }
                $this->redirect('Index/index');
            }else {
                $this->error('密码账户不匹配','login');
            }
        }
        $this->display();
    }
    //退出账户
    public function login_out() {
        session(null);
        $this->redirect('System/login');
    }
    //参与人数设置
    public function joinNum() {
        $data = M('admin_setting')->where('id=1')->find();
        $this->assign('data',$data);
        $this->display();
    }
    public function Add() {
        if ($_POST) {
            $this->success('功能还未完善');
        }else{
            $this->display();
        }
        
    }

    public function deleteUselessMessage(){

        M('message')->where("max_del=1 AND min_del=1")->delete();

    }

    public function GetDatedRedMoney(){
        $list = M('red_money')->where(array('status'=>1,'get_time'=>array('lt',time())))->select();
        if($list){
            
        }
    }

    public function index_photos(){
        
        $this->display();
    }
    public function joinNumUp(){
        if(is_numeric($_POST['red_num']) 
            && is_numeric($_POST['gift']) 
            && is_numeric($_POST['ju_bao']) 
            && is_numeric($_POST['yue_bao']) 
            && is_numeric($_POST['bao_times']) 
            && is_numeric($_POST['tui_red']) 
            && is_numeric($_POST['frize_money']))
        {
            if($_POST['red_rate'] > 1){
                $this->error('获取金额比例不能高于1');
            }
            if($_POST['red_rate'] < 0){
                $this->error('获取金额比例不能低于0');
            }
           $res = M('admin_setting')->where('id=1')->save($_POST);
           if($res === false){
                $this->error('失败');
           }else{
                $this->success('成功','joinNum');
           }
        }else{
            $this->error('所有内容必须是数字');
        }
        
    }

    public function about(){
        if (!isset($_SESSION['admin_username'])) {
            $this->error('未登录');
        }
        $data = M('article')->where(array('type' => 'guanyu'))->find();
        $this->assign('data',$data['content']);
        $this->display();
    }

    public function aboutUp(){
        if (!isset($_SESSION['admin_username'])) {
            $this->error('未登录');
        }
        $content = I('post.content');
        if(empty($content)){
            $this->error("内容为空");
        }

        M('article')->where(array('type' =>'guanyu'))->save(array('content'=>$content));

        $this->success('成功','about');
    }

    // 前端颜色配置
    public function color()
    {   
        if (IS_POST) {

        
            $data = array(
                'page_bac' => I('post.page_bac'),
                'header_footer_bac' => I('post.header_footer_bac'),
                'con_bac' => I('post.con_bac'),
                'layer_bac' => I('post.layer_bac'),
                'user_setting_btn' => I('post.user_setting_btn'),
                'main_line' => I('post.main_line'),
                'deputy_line' => I('post.deputy_line'),
                'main_font' => I('post.main_font'),
                'deputy_font' => I('post.deputy_font'),
                'layer_font' => I('post.layer_font'),
                'activity_btn' => I('post.activity_btn'),
                'chat_receive_bac' => I('post.chat_receive_bac'),
                'chat_receive_font' => I('post.chat_receive_font'),
                'chat_send_bac' => I('post.chat_send_bac'),
                'chat_send_font' => I('post.chat_send_font'),
            );

            $id = M('color_config')->where('id=1')->save($data);

            if ($id) {
                $this->success('成功');
            }else{
                $this->error('失败');
            }

        } else {
            $data = M('color_config')->where('id=1')->find();
            // dump($data);exit;
            $this->assign('data',$data);
            $this->display();
        }
    }
}
